set more off

use dataset, clear

*** covariates ***

local controls dfederalaidpcp dstateaidpcp lpopulation pct_white pct_senior pct_renter lmedianincome 
local economy davgannualwage demployment dunemployment propvaluechange

*** make interactions ***

foreach x in overridep overridel `economy' dfederalaidpcp dstateaidpcp {
	gen demincumbentX`x' = demincumbent*`x'
}
label variable overridep "Tax increase"

*** do the regressions ***

keep if !mi(demvoteshare) & !mi(demincumbentXoverridep)
tabulate year, gen(year_)

foreach x in `controls' `economy' {
	su `x'
	replace `x' = (`x' - `r(min)') / (`r(max)' - `r(min)')
}

gen overridepXlagdemvoteshare = overridep*lagdemvoteshare 

label variable overridepXlagdemvoteshare "Increase X lagged Dem vote"

eststo clear

eststo: xi: reg turnout lagturnout overridepa year_* [aw=totalvotes], cl(town)

eststo: xi: reg turnout lagturnout overridepa `economy' `controls' year_* [aw=totalvotes], cl(town) 

eststo: xi: reg turnout lagturnout overridepa overridepXlagdemvoteshare year_* [aw=totalvotes] if demincumbent == -1, cl(town)

eststo: xi: reg turnout lagturnout overridepa overridepXlagdemvoteshare `economy' `controls' year_* [aw=totalvotes] if demincumbent == -1, cl(town) 

eststo: xi: reg turnout lagturnout overridepa overridepXlagdemvoteshare year_* [aw=totalvotes] if demincumbent == +1, cl(town)

eststo: xi: reg turnout lagturnout overridepa overridepXlagdemvoteshare `economy' `controls' year_* [aw=totalvotes] if demincumbent == +1, cl(town) 

esttab, keep(*overridep*) se compress

#delimit;

esttab
	using "table A4.tex"
	,
		replace
		cells(b(fmt(2) star) se(par))
		eqlabels(none)
		nomtitles
		collabels(none)
		order(overridepassed overridepXlagdemvoteshare davgannualwage demployment propvaluechange dunemployment dfederalaidpcp dstateaidpcp)
		drop(*year*)
		gap
		indicate(
			"Lagged turnout = lagturnout"
			"Year fixed effects = year_1"			
		)
		label
		mlabels(none)
		nonotes
		booktabs
		width(\hsize)
		stats(
			N
			N_clust
			,
				label(
					"Observations"
					"Towns"
						
				)
				fmt(%20.0fc)
		)
		mgroups(
			"All years" "Rep incumbents" "Dem incumbents"
			,
				pattern(1 0 1 0 1 0)
				prefix(\multicolumn{@span}{c}{) suffix(})
				span erepeat(\cmidrule(lr){@span})
		)			
		;
	
#delimit cr
